function [L D] = ldl_chol_v2(A)
    if norm(A - A') > eps
        fprintf("The input matrix must be symetric.\n");
        return;
    end
    n = size(A, 1);
    d = zeros(n, 1);
    L = zeros(n);
    C = zeros(n);
    for j = 1:n
        C(j, j) = A(j, j) - sum(d(1:j - 1)' .* L(j, 1:j - 1).^2);
        d(j) = C(j, j);
        for i = j + 1:n
            C(i, j) = A(i, j) - sum(d(1:j - 1)' .* L(i, 1:j -1) .* L(j, 1:j - 1));
            L(i, j) = C(i, j) / d(j);
        end
        L(j, j) = 1;
    end
    D = diag(d);
end